package org.yang.common.i18n.resolver;

import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;

import java.util.Locale;

/**
 * 自定义国际化解析器
 *
 * @author: Islands
 * @create: 2024-03-01 15:17
 */
@Configuration
public class I18nResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        Locale locale = Locale.CHINA;
        String language = request.getHeader("Accept-Language");
        if (StringUtils.isNotBlank(language)) {
            String[] splitLanguage = language.split("_");
            if (splitLanguage.length > 1) {
                locale = new Locale(splitLanguage[0], splitLanguage[1]);
            }
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }





}




